﻿using System;
using App.Entity;
using App.IServices;
using App.IRepository;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using App.Code;
using System.Data;

/*!
* 文件描述：一切从简，只为了更懒！
*/
namespace App.Services
{
	public partial class Sys_ItemsDetailServices : BaseServices<Sys_ItemsDetail> , ISys_ItemsDetailServices
	{
		ISys_ItemsDetailRepository _ISys_ItemsDetailRepository;
		public Sys_ItemsDetailServices(ISys_ItemsDetailRepository __ISys_ItemsDetailRepository)
		{
			this._ISys_ItemsDetailRepository = __ISys_ItemsDetailRepository;
			base._IBaseRepository = __ISys_ItemsDetailRepository;
		}

        public void DeleteForm(string keyValue)
        {
            _ISys_ItemsDetailRepository.Delete(t => t.Id == keyValue);
        }

        public Sys_ItemsDetail GetForm(string keyValue)
        {
            return _ISys_ItemsDetailRepository.QuerySingle(c=>c.Id==keyValue);
        }

        public List<Sys_ItemsDetail> GetItemList(string enCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT  d.*  FROM    sys_itemsdetail d INNER  JOIN sys_items i ON i.Id = d.ItemId 
WHERE   1 = 1 
AND i.EnCode = @enCode
                                    AND d.EnabledMark = 1
                                    AND d.DeleteMark = 0
                          ORDER BY d.SortCode ASC");
           
            Dictionary<string, object> parameter = new Dictionary<string, object>();
            parameter.Add("@enCode", enCode);
            
            return _ISys_ItemsDetailRepository.QueryBySql(strSql.ToString(),parameter);
        }

        public List<Sys_ItemsDetail> GetList(string itemId = "", string keyword = "")
        {
            var expression = ExtLinq.True<Sys_ItemsDetail>();
            if (!string.IsNullOrEmpty(itemId))
            {
                expression = expression.And(t => t.ItemId == itemId);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.ItemName.Contains(keyword));
                expression = expression.Or(t => t.ItemCode.Contains(keyword));
            }
            return _ISys_ItemsDetailRepository.QueryByWhere(expression, "SortCode");
        }

        public void SubmitForm(Sys_ItemsDetail itemsDetailEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                itemsDetailEntity.Modify(keyValue);
                _ISys_ItemsDetailRepository.Update(itemsDetailEntity);
            }
            else
            {
                itemsDetailEntity.Create();
                _ISys_ItemsDetailRepository.Insert(itemsDetailEntity);
            }
        }
    }
}